var angular = require("angular")

console.log('sssss');

var app =angular.module("myApp",[]);
//创建控制器
app.controller("registerController",function($scope){
  //初始化数据模型
  $scope.username="";
  $scope.usertype="";
  $scope.password ="";
  $scope.refirmpwd="";
  $scope.isagree="";
  $scope.msg="";
  //初始化行为模型
  $scope.register= function(){
    if($scope.username.length<6){
       $scope.msg="用户名太短了!";
       return;
    }
    if($scope.password.length<6){
      $scope.password="密码不能小于6位";
      return;
    }
    if($scope.password !== $scope.refirmpwd){
      $scope.msg="两次输入的密码不相同,请重新输入!!!";
      return;
    }
    var u = new User($scope.username,$scope.password,$scope.usertype);
    var result =u.save();
    if(result){
      $scope.msg="注册成功";
    }else{
      $scope.msg="注册失败";
    }
  }
  //创建一个构造函数
  function User(name,pwd,type){
    this.name=name;
    this.pwd=pwd;
    this.type=type;
  }
  User.prototype.save=function(){
    var str =localStorage.getItem("myusers")|| '[]';
    var arr =JSON.parse(str);
    for(var i=0;i<arr.length;i++){
      if(arr[i].name === this.name){
         return false;
      }
    }
    arr.push({name:this.name,pwd:this.pwd,type:this.type});
    localStorage.setItem("myusers",JSON.stringify(arr));
    return true;
  }

});
